package modele.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import modele.ConnectionManager;
import data.Detaillant;

public class DaoDetaillant extends DAO<Detaillant>{

	// mot de passe cache
	@Override
	public Detaillant find(int id) {		

		String sql = "SELECT * FROM detaillant WHERE username = ?";
		Detaillant d = null;
		PreparedStatement requete;
		try {
			PreparedStatement prest = ConnectionManager.getConnection().prepareStatement(sql);


			requete = prepareStatement(sql);			
			requete.setInt(1, id);	

			ResultSet result = requete.executeQuery();	

			if(result != null && result.next()) {
				d= new Detaillant();
				d.setNom(result.getString("NOM"));
				d.setDescription(result.getString("DESCRIPTION"));
				d.setSiteWeb(result.getString("SITEWEB"));
				d.setUsername(result.getString("USERNAME"));
			}

			requete.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return d;

	}

	@Override
	public void create(Detaillant d) {
		String sql = "INSERT INTO detaillant (nom,description,siteweb,username,psw) VALUES (?,?,?,?,?)";
		PreparedStatement requete;
		try {

			PreparedStatement prest = ConnectionManager.getConnection().prepareStatement(sql);
			requete = prepareStatement(sql);			
			requete.setString(1, d.getNom());	
			requete.setString(2, d.getDescription());
			requete.setString(3, d.getSiteWeb());
			requete.setString(4, d.getUsername());
			requete.setString(5, d.getPsw());

			requete.execute();

			requete.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}



	@Override
	public void update(Detaillant d) {
		String sql = "UPDATE detaillant SET nom=?,description=?,siteweb=?,psw=? WHERE username=? ";
		PreparedStatement requete;
		try {

			PreparedStatement prest = ConnectionManager.getConnection().prepareStatement(sql);
			requete = prepareStatement(sql);			
			requete.setString(1, d.getNom());	
			requete.setString(2, d.getDescription());
			requete.setString(3, d.getSiteWeb());			
			requete.setString(4, d.getPsw());
			
			requete.setString(5, d.getUsername());

			requete.execute();

			requete.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void delete(Detaillant d) {
		String sql = "DELETE FROM detaillant WHERE username=? ";
		
		PreparedStatement requete;
		try {

			PreparedStatement prest = ConnectionManager.getConnection().prepareStatement(sql);
			requete = prepareStatement(sql);			
					
			requete.setString(1, d.getUsername());

			requete.execute();

			requete.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
